package edu.cuit.avatar.array;

import java.util.Arrays;

/**
 * java中参数的传递:值传递
 * @author <a href="mailto:1020zhaodan@163.com">Adan</a>
 * @version 1.0
 * @date 2025/7/3 14:04
 */
public class TestPassByValue {
    public static void main(String[] args) {
        int number = 0;
        int[] list = new int[3];
        System.out.printf("main方法调用change前number = %3d, list[0] = %3d\r\n", number, list[0]); //0, 0
        change(number, list);
        System.out.printf("main方法调用change后number = %3d, list[0] = %3d\r\n", number, list[0]); //0, 200
        System.out.println("===========================");

        int[] array = {10, 20, 30, 40, 50};
        int[] array2 = reverse(array);

        System.out.println(Arrays.toString(array2));
    }

    /**
     * 当一个数组传递给方法时，数组的引用被传递给方法
     * @param number number
     * @param list 数组
     */
    public static void change(int number, int[] list){
        System.out.printf("change改变前number = %3d, list[0] = %3d\r\n", number, list[0]);//0, 0
        number = 200;
        list[0] = 200;
        System.out.printf("change改变后number = %3d, list[0] = %3d\r\n", number, list[0]);//200, 200
    }

    public static int[] reverse(int[] list){
        int[] dest = new int[list.length];
        for(int i = 0, j = dest.length - 1; i < list.length; i++, j--){
            dest[j] = list[i];
        }
        return dest;
    }
}
